package longrise.phone.com.bjjt_henan.Utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Message;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import longrise.phone.com.bjjt_henan.R;


public class WebViewHelper {

    private View view;

    private WebView mWebView;

    private ProgressBar progressBar;

    public WebViewHelper(Context context) {
        if (null == context) {
            return;
        }
        view = View.inflate(context, R.layout.common_webview, null);
        if (null == view) {
            return;
        }
        // 加载进度条
        progressBar = (ProgressBar) view.findViewById(R.id.webview_progress);
        // 加载的页面
        mWebView = (WebView) view.findViewById(R.id.webview);
        mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mWebView.getSettings().setJavaScriptEnabled(true);// 设置支持js
        mWebView.getSettings().setLoadsImagesAutomatically(true);// 设置自动加载图片
        mWebView.setWebChromeClient(new DealChromeClient());// 处理解析、渲染界面
        mWebView.setWebViewClient(new DealViewClient());// 处理通知、请求事件
    }

    // 加载页面
    public void loadUrl(String url) {
        if (null != mWebView) {
            mWebView.loadUrl(url);
        }
    }

    /**
     * 增加js的交互
     *
     * @param object：桥接实例
     * @param name：桥连参数
     */
    @SuppressLint("JavascriptInterface")
    public void addJSInterface(Object object, String name) {
        if (null != mWebView) {
            mWebView.addJavascriptInterface(object, name);
        }
    }

    //获取当前网页地址
    public String getCurrentUrl() {
        if (null != mWebView) {
            return mWebView.getUrl();
        }
        return null;
    }

    //获取原始网页地址
    public String getOriginalUrl() {
        if (null != mWebView) {
            return mWebView.getOriginalUrl();
        }
        return null;
    }

    //获取网页标题
    public String getCurTitle() {
        if (null != mWebView) {
            return mWebView.getTitle();
        }
        return "";
    }

    public View getView() {
        return view;
    }

    // 结束时回收
    public void closeView() {
        if (null != mWebView) {
            mWebView.removeAllViews();
            mWebView.clearHistory();
            mWebView.clearCache(true);
            mWebView.freeMemory();
            mWebView.destroy();
        }
        mWebView = null;
    }

    // 返回上一个界面
    public boolean goBackView() {
        if (null != mWebView && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return false;
    }

    private class DealChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {// 显示加载的进度
            if (null == progressBar) {
                return;
            }
            if (newProgress == 100) {
                progressBar.setVisibility(View.GONE);
            } else {
                if (progressBar.getVisibility() == View.GONE) {
                    progressBar.setVisibility(View.VISIBLE);
                }
                progressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {// 设置网页的标题

        }
    }

    private class DealViewClient extends WebViewClient {
        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {// 页面加载失败的事件
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public void onFormResubmission(WebView view, Message dontResend,
                                       Message resend) {// 重新请求网页数据
            resend.sendToTarget();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {// 对网页中超链接的响应
            if (null != mWebView) {
                mWebView.loadUrl(url);
            }
            return true;
        }
    }
}